package com.vecspace.dayu.code.dao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.vecspace.database.dao.BaseDao;
import org.springframework.stereotype.Repository;

import com.vecspace.dayu.code.pojo.DataColumn;

/** DAYU-字段定义,操作数据库的dao类
 * @author: wanghua
 * @reviewer:
 * @template：wanghua
 */
@Repository("dayu_dataColumnDao")
public class DataColumnDao extends BaseDao {
	/** 根据ID查询 DAYU-字段定义
	 * @param id   主键值
	 * @return DataColumn
	 * @throws Exception
	 * @author wanghua
	 */
	public DataColumn getDataColumn(Long id) throws Exception {
		DataColumn dataColumn = null;
		if (id != null && id != 0) {

			dataColumn = this.queryObject(DataColumn.class, id);
			this.evict(dataColumn);
		}
		return dataColumn;
	}
	
	/**根据表id获取所有字段
	 * 
	 * @param datatableId
	 * @return
	 * @throws Exception 
	 * @author wanghua
	 */
	public List<DataColumn> getDataColumns(Long datatableId) throws Exception {
		List<DataColumn> dataColumns = this.queryObjectList("from DataColumn where datatableId=?0 order by ordnum", new Object[] {datatableId});
		return dataColumns;
	}
	
	/**根据id（多个）查询字段
	 * 
	 * @param tableColumnIds
	 * @return
	 * @throws Exception 
	 * @author wanghua
	 */
	public List<DataColumn> getDataColumns(List<Long> tableColumnIds) throws Exception{
		if(tableColumnIds == null || tableColumnIds.size() == 0)return Collections.emptyList();
		String sql = "select * from DAYU_CODE_DATA_COLUMN t1 where id in";
		String paramPlaceholders = tableColumnIds.stream().map(id -> "?").collect(Collectors.joining(","));
		Object[] paramValues = tableColumnIds.toArray(new Long[tableColumnIds.size()]);
		sql = sql + "("+paramPlaceholders+")";
		List<Map<String, Object>> rows = queryDataRowMapList(sql, paramValues);
		List<DataColumn> columns = new ArrayList<>();
		for(Map<String, Object> row : rows){
			//RowMap rowmap = new RowMap(row);
			DataColumn column = new DataColumn();
			column.parseFrom(row);
			columns.add(column);
		}
		
		return columns;
	}
	
	/** 插入或修改 DAYU-字段定义 
	 * @param item	要保存的对象
	 * @return void
	 * @throws Exception
	 * @author wanghua
	 * @reviewer
	 */
	public void saveDataColumn(DataColumn item) throws Exception {
		this.saveOrUpdateObject(item);
		this.evict(item);
	}

	/**更新单个字段的值
	 * @param columnName
	 * @param id
	 * @param value
	 * @return 返回更新的记录数
	 * @throws Exception 
	 * @author wanghua
	 */
	public int updateValue(Long id, String columnName, Object value) throws Exception {
		String tableName = "DAYU_CODE_DATA_COLUMN";
		String idColumn = "ID";
		return super.updateValue(tableName, idColumn, columnName, id, value);
	}
	
	/** 根据ID删除 DAYU-字段定义 
	 * @param id	主键值
	 * @return void
	 * @throws Exception
	 * @author wanghua
	 */
	public void deleteDataColumn(Long id) throws Exception {
		if (id != null && id != 0) {
			this.deleteObject(DataColumn.class, id);
		}
	}
	
	public void deleteDataColumns(Long tableId) throws Exception {
		if (tableId != null && tableId != 0) {
			
		}
	}
}
